package com.fe.util;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

public class UploadFile {
	
	public static String upload(MultipartFile photo_t) throws IllegalStateException, IOException {
		//要上传到的文件夹
		String root = "E:/upload/familyEducation";
		//获取上传文件的原文件名
		String real_name = photo_t.getOriginalFilename();
		//获取其后缀
		String ext = FilenameUtils.getExtension(real_name);
		//生成新的随即文件名，防止重名
		String newName = UUID.randomUUID().toString().replaceAll("-", "")+"."+ext;
		File file = new File(root+"/"+newName);
		//如果文件夹不存在，则创建它
		if(!file.exists()){
			file.mkdirs();
		}
		//上传
		photo_t.transferTo(file);
		return newName;
	}
}
